package aom.ju.ss.utils

import android.app.Activity
import android.view.View
import aom.ju.ss.databinding.OrderListBtBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.TuikuangoodsInfo
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.LP_API.cancel_order
import aom.ju.ss.utils.http.LP_API.confirm_receipt
import aom.ju.ss.utils.http.LP_API.order_after_sale_cancel
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.pay.WxXcxzf
import aom.ju.ss.view.dialog.TitleDialog
import aom.ju.ss.view.pay.PayActivity
import aom.ju.ss.view.user.userOrder.RequestARefund
import com.google.gson.Gson
import com.scwang.smartrefresh.layout.SmartRefreshLayout

/**
 * 用户订单操作
 */


class OrderBtList(
    goods: TuikuangoodsInfo,
    type: String,
    unpaid: String,//支付金额
    orderNo: String,
    after_sale_status: Int,//是否售后中 1 是
    status: Int,
    b: OrderListBtBinding,
    var activity: Activity,
    var smart: SmartRefreshLayout? = null
) : MyHttpCallBack {
    init {

        b.afterSaleDetails.VG(false)
        b.cancelOrder.VG(false)
        b.editAddress.VG(false)
        b.pay.VG(false)
        b.lookLogistics.VG(false)
        b.deleteOrder.VG(false)
        b.requestRefund.VG(false)
        b.getGoods.VG(false)
        b.cancelAfterSale.VG(false)

// "", "待支付", "待发货", "待签收", "待评论", "", "","已退款（订单商品全部已退）","已完成","已取消",""

        if (after_sale_status == 1) {
            b.cancelAfterSale.VG(true)
        }
        when (status) {
            1 -> {
                b.pay.VG(true)
                b.cancelOrder.VG(true)
            }
            2 -> {
                b.requestRefund.VG(true)
//                b.editAddress.VG(true)
            }
            3 -> {
                b.requestRefund.VG(true)
                b.lookLogistics.VG(true)
                b.getGoods.VG(true)
            }
            4 -> {

            }
            7, 8, 9 -> {
                b.deleteOrder.VG(true)
            }

        }


        b.afterSaleDetails.setOnClickListener { }//售后详情
        b.cancelOrder.setOnClickListener {//取消订单
            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (!IsDetermine) {
                        return
                    }
                    OKHttpUtils(activity)
                        .SetApiUrl(cancel_order)
                        .SetKey("id")
                        .SetValue(orderNo)
                        .POST(this@OrderBtList)
                }
            }).setConnect("确定取消订单?")
        }
        b.editAddress.setOnClickListener { }//修改地址 暂无api
        b.lookLogistics.setOnClickListener { }//查看物流 暂无api
        b.deleteOrder.setOnClickListener { }//删除订单 暂无api

        b.requestRefund.setOnClickListener {
            activity.start(RequestARefund::class.java, Gson().toJson(goods))
        }//申请退款

        b.pay.setOnClickListener {//付款
            activity.startResult(
                PayActivity::class.java,1999, arrayListOf(
                    Gson().toJson(
                        WxXcxzf(
                            business_type = "0",
                            pay_type = "4",
                            order_id = orderNo,
                            apitype = "0",
                            api = LP_API.get_pay_info,
                            allPrice = unpaid
                        )
                    ), type
                )
            )

        }
        b.getGoods.setOnClickListener {
            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (!IsDetermine) {
                        return
                    }
                    OKHttpUtils(activity)
                        .SetApiUrl(confirm_receipt)
                        .SetKey("order_id")
                        .SetValue(orderNo)
                        .POST(this@OrderBtList)
                }
            }).setConnect("确定收到货?")
        }//确认收货
        b.cancelAfterSale.setOnClickListener {
            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (!IsDetermine) {
                        return
                    }
                    OKHttpUtils(activity)
                        .SetApiUrl(order_after_sale_cancel)
                        .SetKey("id")
                        .SetValue(orderNo)
                        .POST(this@OrderBtList)
                }
            }).setConnect("确定取消售后?")
        }//取消售后
    }

    override fun ok(jsonString: String?, httpTY: String?) {
        when (httpTY) {
            cancel_order -> {
                "取消成功".show
                smart?.autoRefresh()
            }
            confirm_receipt -> {
                "收货成功".show
                smart?.autoRefresh()
            }
            order_after_sale_cancel -> {
                "取消售后成功".show
                smart?.autoRefresh()
            }
        }


    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        e?.show

    }
}